package com.jsg.sl651.enums;

import lombok.Getter;

/**
 * ASCII码对照枚举
 */
@Getter
public enum ASCIICode {
    NUL(0, "\u0000"),    // Null
    SOH(1, "\u0001"),    // Start of Heading
    STX(2, "\u0002"),    // Start of Text
    ETX(3, "\u0003"),    // End of Text
    EOT(4, "\u0004"),    // End of Transmission
    ENQ(5, "\u0005"),    // Enquiry
    ACK(6, "\u0006"),    // Acknowledgment
    BEL(7, "\u0007"),    // Bell
    BS(8, "\u0008"),     // Backspace
    HT(9, "\u0009"),     // Horizontal Tab
    LF(10, "\n"),        // Line Feed
    VT(11, "\u000B"),    // Vertical Tab
    FF(12, "\f"),        // Form Feed
    CR(13, "\r"),        // Carriage Return
    SO(14, "\u000E"),    // Shift Out
    SI(15, "\u000F"),    // Shift In
    DLE(16, "\u0010"),   // Data Link Escape
    DC1(17, "\u0011"),   // Device Control 1 (XON)
    DC2(18, "\u0012"),   // Device Control 2
    DC3(19, "\u0013"),   // Device Control 3 (XOFF)
    DC4(20, "\u0014"),   // Device Control 4
    NAK(21, "\u0015"),   // Negative Acknowledgment
    SYN(22, "\u0016"),   // Synchronous Idle
    ETB(23, "\u0017"),   // End of Transmission Block
    CAN(24, "\u0018"),   // Cancel
    EM(25, "\u0019"),    // End of Medium
    SUB(26, "\u001A"),   // Substitute
    ESC(27, "\u001B"),   // Escape
    FS(28, "\u001C"),    // File Separator
    GS(29, "\u001D"),    // Group Separator
    RS(30, "\u001E"),    // Record Separator
    US(31, "\u001F"),    // Unit Separator
    SPACE(32, " "),      // Space
    EXCLAMATION_MARK(33, "!"),
    QUOTATION_MARK(34, "\""),
    NUMBER_SIGN(35, "#"),
    DOLLAR_SIGN(36, "$"),
    PERCENT_SIGN(37, "%"),
    AMPERSAND(38, "&"),
    APOSTROPHE(39, "'"),
    LEFT_PARENTHESIS(40, "("),
    RIGHT_PARENTHESIS(41, ")"),
    ASTERISK(42, "*"),
    PLUS_SIGN(43, "+"),
    COMMA(44, ","),
    HYPHEN_MINUS(45, "-"),
    FULL_STOP(46, "."),
    SOLIDUS(47, "/"),
    DIGIT_ZERO(48, "0"),
    DIGIT_ONE(49, "1"),
    DIGIT_TWO(50, "2"),
    DIGIT_THREE(51, "3"),
    DIGIT_FOUR(52, "4"),
    DIGIT_FIVE(53, "5"),
    DIGIT_SIX(54, "6"),
    DIGIT_SEVEN(55, "7"),
    DIGIT_EIGHT(56, "8"),
    DIGIT_NINE(57, "9"),
    COLON(58, ":"),
    SEMICOLON(59, ";"),
    LESS_THAN_SIGN(60, "<"),
    EQUALS_SIGN(61, "="),
    GREATER_THAN_SIGN(62, ">"),
    QUESTION_MARK(63, "?"),
    COMMERCIAL_AT(64, "@"),
    LATIN_CAPITAL_LETTER_A(65, "A"),
    LATIN_CAPITAL_LETTER_B(66, "B"),
    LATIN_CAPITAL_LETTER_C(67, "C"),
    LATIN_CAPITAL_LETTER_D(68, "D"),
    LATIN_CAPITAL_LETTER_E(69, "E"),
    LATIN_CAPITAL_LETTER_F(70, "F"),
    LATIN_CAPITAL_LETTER_G(71, "G"),
    LATIN_CAPITAL_LETTER_H(72, "H"),
    LATIN_CAPITAL_LETTER_I(73, "I"),
    LATIN_CAPITAL_LETTER_J(74, "J"),
    LATIN_CAPITAL_LETTER_K(75, "K"),
    LATIN_CAPITAL_LETTER_L(76, "L"),
    LATIN_CAPITAL_LETTER_M(77, "M"),
    LATIN_CAPITAL_LETTER_N(78, "N"),
    LATIN_CAPITAL_LETTER_O(79, "O"),
    LATIN_CAPITAL_LETTER_P(80, "P"),
    LATIN_CAPITAL_LETTER_Q(81, "Q"),
    LATIN_CAPITAL_LETTER_R(82, "R"),
    LATIN_CAPITAL_LETTER_S(83, "S"),
    LATIN_CAPITAL_LETTER_T(84, "T"),
    LATIN_CAPITAL_LETTER_U(85, "U"),
    LATIN_CAPITAL_LETTER_V(86, "V"),
    LATIN_CAPITAL_LETTER_W(87, "W"),
    LATIN_CAPITAL_LETTER_X(88, "X"),
    LATIN_CAPITAL_LETTER_Y(89, "Y"),
    LATIN_CAPITAL_LETTER_Z(90, "Z"),
    LEFT_SQUARE_BRACKET(91, "["),
    REVERSE_SOLIDUS(92, "\\"),
    RIGHT_SQUARE_BRACKET(93, "]"),
    CIRCUMFLEX_ACCENT(94, "^"),
    LOW_LINE(95, "_"),
    GRAVE_ACCENT(96, "`"),
    LATIN_SMALL_LETTER_A(97, "a"),
    LATIN_SMALL_LETTER_B(98, "b"),
    LATIN_SMALL_LETTER_C(99, "c"),
    LATIN_SMALL_LETTER_D(100, "d"),
    LATIN_SMALL_LETTER_E(101, "e"),
    LATIN_SMALL_LETTER_F(102, "f"),
    LATIN_SMALL_LETTER_G(103, "g"),
    LATIN_SMALL_LETTER_H(104, "h"),
    LATIN_SMALL_LETTER_I(105, "i"),
    LATIN_SMALL_LETTER_J(106, "j"),
    LATIN_SMALL_LETTER_K(107, "k"),
    LATIN_SMALL_LETTER_L(108, "l"),
    LATIN_SMALL_LETTER_M(109, "m"),
    LATIN_SMALL_LETTER_N(110, "n"),
    LATIN_SMALL_LETTER_O(111, "o"),
    LATIN_SMALL_LETTER_P(112, "p"),
    LATIN_SMALL_LETTER_Q(113, "q"),
    LATIN_SMALL_LETTER_R(114, "r"),
    LATIN_SMALL_LETTER_S(115, "s"),
    LATIN_SMALL_LETTER_T(116, "t"),
    LATIN_SMALL_LETTER_U(117, "u"),
    LATIN_SMALL_LETTER_V(118, "v"),
    LATIN_SMALL_LETTER_W(119, "w"),
    LATIN_SMALL_LETTER_X(120, "x"),
    LATIN_SMALL_LETTER_Y(121, "y"),
    LATIN_SMALL_LETTER_Z(122, "z"),
    LEFT_CURLY_BRACKET(123, "{"),
    VERTICAL_LINE(124, "|"),
    RIGHT_CURLY_BRACKET(125, "}"),
    TILDE(126, "~"),
    DELETE(127, "\u007F"); // Delete

    private final int decimal;
    private final char character;

    ASCIICode(int decimal, String character) {
        this.decimal = decimal;
        this.character = character.charAt(0);
    }

    /**
     * 根据十进制ASCII码获取对应的枚举项
     *
     * @param decimal 十进制ASCII码
     * @return 对应的枚举项
     * @throws IllegalArgumentException 如果输入的ASCII码不在0到127之间
     */
    public static ASCIICode fromDecimal(int decimal) {
        if (decimal < 0 || decimal > 127) {
            throw new IllegalArgumentException("ASCII码必须在0到127之间");
        }
        return values()[decimal];
    }

    /**
     * 根据字符获取对应的枚举项
     *
     * @param character 字符
     * @return 对应的枚举项
     */
    public static ASCIICode fromCharacter(char character) {
        for (ASCIICode code : values()) {
            if (code.getCharacter() == character) {
                return code;
            }
        }
        throw new IllegalArgumentException("无效的字符");
    }
}
